package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Map;

/*
类简介：

功能：

时间：2020/4/6

*/
@RequestMapping("order")
@ResponseBody
@Controller
public class OrderController {

    @Autowired
    JedisPool jedisPool;

    @Reference
    private MemberService memberService;
    @Reference
    private OrderService orderService;

    @RequestMapping("submit")
    public Result sendValidateCode(@RequestBody Map map){

        String telephone = (String)map.get("telephone");

        //从redis获取验证码
        String code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        String validateCode = (String)map.get("validateCode");

        System.out.println("code = " + code);
        System.out.println("validateCode = " + validateCode);

        if (validateCode != null && !validateCode.equals("") && validateCode.equals(code)){

            //获取预约日期
            Date orderDate;
            try {
                orderDate = DateUtils.parseString2Date((String) map.get("orderDate"));
            } catch (Exception e) {
                return new Result(false, "日期解析错误");
            }

            //先判定当天人数是否已满
            if (orderService.isFull(orderDate) == 0){
                return new Result(false, MessageConstant.ORDER_FULL);
            }

            //验证码验证成功
            Member member = new Member();
            member.setName((String) map.get("name"));
            member.setSex((String) map.get("sex"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));

            try {
                //新增会员  如果存在就直接返回会员id
                member.setId(memberService.addMember(member));
            }catch (Exception e){
                return new Result(false, e.getMessage());
            }

            int setmealId = Integer.parseInt(map.get("setmealId").toString());

            Order order = new Order(member.getId(), orderDate, Order.ORDERTYPE_WEIXIN, Order.ORDERSTATUS_NO, setmealId);

            int orderId;

            try {
                orderId = orderService.addOrder(order);
            }catch (Exception e){
                return new Result(false, e.getMessage());
            }
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS,orderId);
        }else {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    @RequestMapping("findById")
    public Result findById(int id){
        Map<String , Object> map;
        try {
            map = orderService.findById(id);
        }catch (Exception  e){
            return new Result(false, "网络繁忙");
        }

        return new Result(true, MessageConstant.ORDER_SUCCESS,map);
    }

}
